<?php
class Shop_Model_Search extends Promotor_Model_Scaffold {

	protected $_dbTableClass = 'Shop_Model_DbTable_Product';
	
	protected $_cachedMethods = array(
		'find',
		'getAttr'
	);
		
	protected $_price = array();
	
	public function setPrice($price) {
		
	}
	
	public function getAttr(array $attrs)
	{
		$table = $this->getDbTable();
		$db = $table->getAdapter();

		$select = new Zend_Db_Select($db);
		$select->from(array('pav' => 'shop_product_attr_val'), array('product_id'))
				->distinct(true)
				->joinInner(
			   		array('pa' => 'shop_product_attr'),
			   		'pav.attr_id = pa.id',
			   		array()
			   	);

		$find = false;
		foreach ($attrs as $alias => $value)
		{
			if (strlen($value) > 0)
			{
				$find = true;
				$where = $db->quoteInto('pa.alias = ?', $alias);
				$select->orWhere($where . ' AND pav.value LIKE ?','%' .$value. '%');
			}
		}

		if ($find === false) {
			return;
		}
		
		try {
			$stmt = $select->query();
			$this->_setStatus(self::SUCCESS);
		} catch(Zend_Date_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
		
		$rowset = array();
		while($row = $stmt->fetchColumn())
		{
			$rowset[] = $row;			
		}

		return $rowset;
	}

	public function find($name, $price = null, $attr = null, $page = null, $onPage = null) {
		$table = $this->getDbTable();
		$db = $table->getAdapter();
		
		$productIds = null;
		if (is_array($attr) && count($attr))
		{
			$productIds = $this->getAttr($attr);
		}
		
		$select = new Zend_Db_Select($db);
		$select->distinct(true);
		$select->from(array('shc' => 'shop_has_category'), array())
			   ->joinInner(
			   		array('sp' => 'shop_product'),
			   		'shc.product_id = sp.id',
			   		array(
			   			'*',
			   			'promotion' => new Zend_Db_Expr('CURRENT_DATE BETWEEN spp.t_start AND spp.t_end')
			   		)
			   	)

			   	// tylko produkty gdzie producent jest widoczny
			   	->joinInner(
			   		array('sm' => 'shop_manufacturer'),
			   		'sm.id = sp.manufacturer_id ',
//			   		'AND sm.publicated = 1',
			   		array())
			   	
			   	// tylko produkty gdzie kategoria jest widoczna
			   	->joinInner(
			   		array('sc' => 'shop_category'),
			   		'sc.id = shc.category_id ',
//			   		'AND sc.publicated = 1',
			   		array())
			   		
//			   	->joinLeft(
//			   		array('sc' => 'shop_product_attr'),
//			   		'sc.id = shc.category_id ',

			   	// czy produkt jest w promocji
			   	->joinLeft(
			   		array('spp' => 'shop_promotion'),
			   		'spp.product_id = sp.id',
			   		array('modifer','value'))

			   	// dołączenie grafiki głównej
			   	->joinLeft(
			   		array('si' => 'shop_image'),
			   		'si.id = sp.image_id',
			   		array('image_name' => 'name','image'))

			   	// gdzie produkt jest publikowany
			   	->where('sp.publicated = 1')
			   	
			   	// gdzie producent jest widoczny
			   	->where('sm.publicated = 1')

			   	// gdzie kategoria jest widoczna
			   	->where('sc.publicated = 1')

			   	// tylko produkty które nie są w produkcji lub produkty których prmocja jest w przedziale
			   	->where('spp.t_end IS NULL OR CURRENT_DATE BETWEEN spp.t_start AND spp.t_end')

			   	// sortuowanie od najmniejszej wartosci do największej
			   	->order('shc.idx ASC');

		if (strlen($name) > 0)
		{
			$select->where('sp.name LIKE ?', '%'.$name.'%');
		}
			   	
		if (is_array($price))
		{
			if (isset($price['min']) && is_numeric($price['min'])
				&& isset($price['max']) && is_numeric($price['max']))
			{
				$where = $db->quoteInto('sp.price BETWEEN ?', $price['min'], Zend_Db::INT_TYPE);
				$where = $db->quoteInto($where . ' AND ?', $price['max'], Zend_Db::INT_TYPE);
				$select->where($where);
			} else
			// cena minimalna
			if (isset($price['min']) && is_numeric($price['min']))
			{
				$where = $db->quoteInto('sp.price > ?', $price['min'], Zend_Db::INT_TYPE);
				$select->where($where);
			} else
			// cena maksymalna
			if (isset($price['max']) && is_numeric($price['max']))
			{
				$where = $db->quoteInto('sp.price < ?', $price['max'], Zend_Db::INT_TYPE);
				$select->where($where);
			}
			
		} else
		if (is_numeric($price)) {
			$where = $db->quoteInto('sp.price = ?', $price['min'], Zend_Db::INT_TYPE);
			$select->where($where);
		}

		
		if (is_array($productIds)) {
			$select->where('sp.id IN(' . implode(',', $productIds) .')');
		}
		
		if (is_numeric($page) && is_numeric($onPage)) {
			$select->limitPage($page, $onPage);
		}

		try {
			$stmt = $select->query();
			$rowset = $stmt->fetchAll();
			$this->_setStatus(self::SUCCESS);
			return array($rowset, $select);
		} catch(Zend_Date_Exception $e) {
			$this->_addException($e);
			$this->_setStatus(self::FAILURE);
		}
	}
}